/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.admin.model;

import com.common.json.JsonModel;
import com.entities.Employee;
import com.entities.EventLibrary;
import com.entities.EventRegistration;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.StoredProcedureQuery;
import org.json.simple.JSONObject;

/**
 *
 * @author Tuan Long
 */
@Stateless
public class EventLibraryFacade extends AbstractFacade<EventLibrary> implements EventLibraryFacadeLocal {

    @PersistenceContext(unitName = "BluePumpkinEJBPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public EventLibraryFacade() {
        super(EventLibrary.class);
    }

    /**
     * @EventLibraryFacade.fillEventsDataTable: return to List< -JSONObject
     * String->, with a JSONObject is the row-result of the Select statement
     * from database
     * @return List<>
     */
    @Override
    public List<String> fillEventsDataTable() {
        List<String> jsonlist = new ArrayList<String>();
        Query showAll = em.createNamedStoredProcedureQuery("EventLibrary.fillEventsDataTable");
        List<Object[]> results = showAll.getResultList();
        for (Object[] row : results) {
            int eventId = (int) row[0];
            String eventName = (String) row[1];
            String description = (String) row[2];
            String eventType = (String) row[3];
            Date timeS = (Date) row[4];
            Date timeE = (Date) row[5];
            int registered = (int) row[6];
            EventLibrary el = new EventLibrary(eventId, eventName, description, eventType, timeS, timeE);
            JSONObject jsonObj = JsonModel.eventsDataTableMessageSender(el, registered);
            String json = jsonObj.toString();
            jsonlist.add(json);
        }
        return jsonlist;
    }

    @Override
    public List<String> testUseSPParameter(String name) {
        List<String> jsonlist = new ArrayList<String>();
        StoredProcedureQuery query = em.createNamedStoredProcedureQuery("EventLibrary.testParameter");
        query.setParameter("name", name);
        List<Object[]> results = query.getResultList();
        for (Object[] row : results) {
            int eventId = (int) row[0];
            String eventName = (String) row[1];
            String description = (String) row[2];
            String eventType = (String) row[3];
            Date timeS = (Date) row[4];
            Date timeE = (Date) row[5];
            EventLibrary el = new EventLibrary(eventId, eventName, description, eventType, timeS, timeE);
            JSONObject jsonObj = JsonModel.eventMessageSender(el);
            String json = jsonObj.toString();
            jsonlist.add(json);
        }
        return jsonlist;
    }

    /**
     * @EventLibraryFacade.fillRegisteredTable: return to List< -JSONObject
     * String->, with a JSONObject is the row-result of the Select statement
     * from database
     * @return List<>
     */
    @Override
    public List<String> fillRegisteredTable() {
        List<String> jsonlist = new ArrayList<String>();
        StoredProcedureQuery query = em.createNamedStoredProcedureQuery("EventLibrary.fillRegisteredTable");
        List<Object[]> results = query.getResultList();
        for (Object[] row : results) {
            String eventName = (String) row[0];
            String username = (String) row[1];
            Employee e = new Employee(username);
            Date timeRegistered = (Date) row[2];
            EventRegistration er = new EventRegistration();
            er.setUsername(e);
            er.setTimeRegisted(timeRegistered);
            JSONObject jsonObj = JsonModel.eventRegistrationSender(er, eventName);
            String json = jsonObj.toString();
            jsonlist.add(json);
        }
        return jsonlist;
    }

    /**
     * @EventLibraryFacade.fillTotalRegistered: return to List< -JSONObject
     * String->, with a JSONObject is the row-result of the Select statement
     * from database
     * @return List<>
     */
    @Override
    public List<String> fillTotalRegistered() {
        List<String> jsonlist = new ArrayList<String>();
        StoredProcedureQuery query = em.createNamedStoredProcedureQuery("EventLibrary.totalRegistered");
        List<Object[]> results = query.getResultList();
        for (Object[] row : results) {
            String username = (String) row[0];
            int total = (int) row[1];
            String total_str = "" + total;
            JSONObject jsonObj = JsonModel.eventTotalRegistrationSender(username, total_str);
            String json = jsonObj.toString();
            jsonlist.add(json);
        }
        return jsonlist;
    }

    /**
     * @EventLibraryFacade.fillTotalEventTypeTable: return to List< -JSONObject
     * String->, with a JSONObject is the row-result of the Select statement
     * from database
     * @return List<>
     */
    @Override
    public List<String> fillTotalEventTypeTable() {
        List<String> jsonlist = new ArrayList<String>();
        StoredProcedureQuery query = em.createNamedStoredProcedureQuery("EventLibrary.totalEventType");
        List<Object[]> results = query.getResultList();
        for (Object[] row : results) {
            String eventtype = (String) row[0];
            int total = (int) row[1];
            String total_str = "" + total;
            JSONObject jsonObj = JsonModel.eventTotalEventTypeSender(eventtype, total_str);
            String json = jsonObj.toString();
            jsonlist.add(json);
        }
        return jsonlist;
    }

    /**
     *
     * @return List<JSONObject-String>
     */
    @Override
    public List<String> readEventComments() {
        List<String> jsonlist = new ArrayList<String>();
        StoredProcedureQuery query = em.createNamedStoredProcedureQuery("EventLibrary.readEventComments");
        List<Object[]> results = query.getResultList();
        for (Object[] row : results) {
            int id = (int) row[0];
            String eventID = "" + id;
            String name = (String) row[1];
            String user = (String) row[2];
            String comment = (String) row[3];
            Date commented = (Date) row[4];
            JSONObject jsonObj = JsonModel.readEventCommentsSender(eventID, name, user, comment, commented);
            String json = jsonObj.toString();
            jsonlist.add(json);
        }
        return jsonlist;
    }

    /**
     * @param eventId Integer
     * @return String event content
     */
    @Override
    public String getEventContentById(int eventId) {
        StoredProcedureQuery query = em.createNamedStoredProcedureQuery("EventLibrary.getEventContentById");
        query.setParameter("eventID", eventId);
        Object[] row = (Object[]) query.getSingleResult();
        int id = (int) row[0];
        String name = (String) row[1];
        String des = (String) row[2];
        String type = (String) row[3];
        Date timeS = (Date) row[4];
        Date timeE = (Date) row[5];
        int comments = (int) row[6];
        JSONObject jsonObj = JsonModel.getEventContentByIdSender(id, name, des, type, timeS, timeE, comments);
        String json = jsonObj.toString();
        return "" + json;
    }

    /**
     * @param eventType String
     * @return List<String>
     */
    @Override
    public List<String> getTop10EventIdAndNameByEventTag(String eventType) {
        List<String> jsonlist = new ArrayList<String>();
        if (eventType != null) {
            Query query = em.createNamedQuery("EventLibrary.getTop10EventIdAndNameByEventType");
            query.setParameter("eventType", eventType);
            query.setMaxResults(20);
            List<Object[]> results = query.getResultList();
            for (Object[] row : results) {
                int id = (int) row[0];
                String eventName = (String) row[1];
                JSONObject jsonObj = JsonModel.getTop10EventIdAndNameSender(id, eventName);
                String json = jsonObj.toString();
                jsonlist.add(json);
            }
        }
        return jsonlist;
    }

    @Override
    public List<String> getTop10EventIdAndName() {
        List<String> jsonlist = new ArrayList<String>();
        Query query = em.createNamedQuery("EventLibrary.getTop10EventIdAndName");
        query.setMaxResults(20);
        List<Object[]> results = query.getResultList();
        for (Object[] row : results) {
            int id = (int) row[0];
            String eventName = (String) row[1];
            JSONObject jsonObj = JsonModel.getTop10EventIdAndNameSender(id, eventName);
            String json = jsonObj.toString();
            jsonlist.add(json);
        }
        return jsonlist;
    }

    /**
     * @param eventId Integer
     * @return String Description return Json-string
     */
    @Override
    public String getUpdateEventInformationById(int eventId) {
        StoredProcedureQuery query = em.createNamedStoredProcedureQuery("EventLibrary.getUpdateEventInformationById");
        query.setParameter("eventId", eventId);
        Object[] row = (Object[]) query.getSingleResult();
        int id = (int) row[0];
        String name = (String) row[1];
        String des = (String) row[2];
        String type = (String) row[3];
        Date timeS = (Date) row[4];
        Date timeE = (Date) row[5];
        String avatar = (String) row[6];
        String shortdes = (String) row[7];
        Date created = (Date) row[8];
        String address = "Ha Noi, Viet Nam";
        if (row[9] != null) {
            address = (String) row[9];
        }
        String json = JsonModel.getUpdateEventInformationByIdSender(id, name, des, type, timeS, timeE, avatar, shortdes, created, address);
        return "" + json;
    }

    @Override
    public List<String> getEventComming(int day) {
        List<String> list = new ArrayList<String>();
        StoredProcedureQuery query = em.createNamedStoredProcedureQuery("EventLibrary.getEventComming");
        query.setParameter("day", day);
        List<Object[]> results = query.getResultList();
        for (Object[] row : results) {
            int id = (int) row[0];
            String name = (String) row[1];
            String des = (String) row[2];
            String type = (String) row[3];
            Date timeS = (Date) row[4];
            Date timeE = (Date) row[5];
            String avatar = (String) row[6];
            String shortdes = (String) row[7];
            Date created = (Date) row[8];
            String address = "Ha Noi, Viet Nam";
            if (row[9] != null) {
                address = (String) row[9];
            }
            int total = 0;
            if (row[10] != null) {
                total = (int) row[10];
            }
            String json = JsonModel.getEventCommingSender(id, name, des, type, timeS, timeE, avatar, shortdes, created, address,total);
            list.add(json);
        }
        return list;
    }

}
